<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use think\Controller;
use think\Db;
use app\common\model\Config as ConfigModel;
use think\Cookie;
/**
 * 登录控制器
 * Class Login
 * @package app\home\controller
 */
class Login extends Controller
{
    public function getCc()
    {
        $ip = '117.136.100.214';
        $address = getipaddress($ip);
        $address = json_decode($address,true); //数据转换
        echo '<pre>';
        print_r($address);die;
        $appid = config('home.appid');//开放平台appid
        $mch_id = config('home.cmch_id');  //商户号
        $key = config('home.cmch_secret');  //商户密钥
        $recharge = Db::name('recharge')->field('id,myorderno,ordertime')->where(['pstates'=>1])->select();
//        $recharge = Db::name('recharge')->field('id,myorderno')->where(['id'=>3312])->select();

        foreach($recharge as &$v){
//            $weixinpay = new \WeixinPay\WeixinPay($appid,$openid = '',$mch_id,$key,$v['myorderno'],$body = '',$total_fee = '');
//            $return = $weixinpay->check();
//            if($return['result_code'] == "SUCCESS" && $return['trade_state'] == 'SUCCESS'){
                Db::name('recharge')->where('id',$v['id'])->update(['paytime'=>$v['ordertime']]);
//            }
        }
        echo '成功';die;
        //create_qrcode('http://www.baidu.com','123.png','logo.png');die;
        $mem_res['img'] = 'http://fushan.oss-cn-hangzhou.aliyuncs.com/headimg/FS55ed282cd55f7e24506b1663eace32e6';
        $result = 'headimg/F'.substr($mem_res['img'],strripos($mem_res['img'],"FS")+1);
//        $a = 'http://fushan.oss-cn-hangzhou.aliyuncs.com/headimg/FS55ed282cd55f7e24506b1663eace32e6';
//        $aa = explode('/',$a);
        echo '<pre>';
        print_r($result);die;
        header("content-type:image/jpg");
        $name = "莫言情难忘";
        $content1 = "南无阿弥陀佛！";
        $content2 = '师兄，想要了解更多佛教内容，';
        $content3 = '扫描右边的二维码，我在';
        $content4 = '福善';
        $content5 = 'Online';
        $content6 = '等您！';
        $content7 = '让信仰者回归信仰';
        $im = imagecreatetruecolor(640, 1060); // 设置画布
        $color = imagecolorallocate($im, 245, 244, 249); //为画布分配颜色
        imagefill($im,0,0,$color); //填充颜色
        $font = ROOT_PATH.'public\static\font\msyh.ttf';               // 设置字体
        $blacka = imagecolorallocate($im, 15, 23, 25);  // 颜色
        $blacka1 = imagecolorallocate($im, 239, 167, 59);  // 颜色
        imagettftext($im, 20, 0, 120, 100, $blacka, $font,$name );       // 写入名称
        imagettftext($im, 18, 0, 30, 560, $blacka, $font, $content1);      // 写入积分
        imagettftext($im, 18, 0, 30, 600, $blacka, $font, $content2);      // 写入积分
        imagettftext($im, 18, 0, 30, 640, $blacka, $font, $content3);      // 写入积分
        imagettftext($im, 18, 0, 295, 640, $blacka1, $font, $content4);      // 写入积分
        imagettftext($im, 18, 0, 30, 680, $blacka1, $font, $content5);      // 写入积分
        imagettftext($im, 18, 0, 110, 680, $blacka, $font, $content6);      // 写入积分
        imagettftext($im, 18, 0, 300, 800, $blacka, $font, $content7);      // 写入积分

        $upload = new Upload();
        $imgurl = $upload->save('goods681586311665040');

        $image = \think\Image::open($imgurl);
        $file = ROOT_PATH.'public\img\\'.'f'.time().rand(1,100).'.jpg';
        $image->thumb(640, 1060)->save($file);
        $src_info = getimagesize($file);
        $src_im = imagecreatefromjpeg($file);
        $file1 = $this->imagemerge($im,$src_im,0,180,0,0,$src_info[0], $src_info[1],100);

        $file2 = ROOT_PATH.'public\fushan.png';
        $dst_im1 = imagecreatefromjpeg($file1);
        $src_im1 = imagecreatefrompng($file2);
        $src_info1 = getimagesize($file2);
        $file3 = $this->imagemerge($dst_im1,$src_im1,200,770,0,0,$src_info1[0], $src_info1[1],100);

        $qrcode = Qrcode('1100','https://www.baidu.com');
        $file4 = ROOT_PATH.'public\\'.$qrcode;
        $dst_im2 = imagecreatefromjpeg($file3);
        $src_im2 = imagecreatefrompng($file4);
        $src_info2 = getimagesize($file4);
        $file5 = $this->imagemerge($dst_im2,$src_im2,380,530,0,0,$src_info2[0], $src_info2[1],100);

        unlink($file);
        unlink($file1);
        unlink($file3);
        die;
    }
    public function imagemerge($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$alpha)
    {
        imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $alpha);
        $file = ROOT_PATH.'public\img\\'.time().rand(1,99).'.jpg';
        imagejpeg($dst_im,$file);     // 生成jpeg格式图片
        imagedestroy($dst_im);  // 销毁图片
        return $file;
    }
    public function getAa()
    {

        $accessKeyId = "LTAIUZZ0EO0Xlz0c";
        $accessKeySecret = "ZPPOxFBERpKvaw4uO5oZVbdgnd5cRd";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "oss-cn-hangzhou.aliyuncs.com";
        $bucket= "fushan";
        $object = "4.png";

        try{
            $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->deleteObject($bucket, $object);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }
    /**
     * [postLogin  中慧科技公众号智慧寺院微信授权登录]
     * @param
     * @author [C。]
     * @return json
     */
    public function postLogin()
    {
        $param = input('param.');
        $model = new ConfigModel;
        $appid = $model->findData('zh_appid','cname','cvalue');
        $secret = $model->findData('zh_secret','cname','cvalue');
        $code = $param['code'];
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid['cvalue'] . '&secret=' . $secret['cvalue'] . '&code=' . $code . '&grant_type=authorization_code';
        $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据，推荐使用curl
        $json = json_decode($info);//对json数据解码
        $arr = get_object_vars($json);
        if (empty($arr['access_token'])) {
            return admin_json('-1', $arr);
        }
        $access_token = $arr['access_token'];
        $openid = $arr['openid'];
        $user_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
        $user_info = file_get_contents($user_url);//发送HTTPs请求并获取返回的数据，推荐使用curl
        $user_json = json_decode($user_info);//对json数据解码
        $user_arr = get_object_vars($user_json);
        $user_arr['nickname'] = filter_Emoji($user_arr['nickname']);
        $wechat = array(
            'openid' => $user_arr['openid'],
            'unionid' => $user_arr['unionid'],
            'headimgurl' => $user_arr['headimgurl'],
            'nickname' => $user_arr['nickname'],
            'sex' => $user_arr['sex'],
        );
        $result = Db::name('wechat')->where(['unionid' => $user_arr['unionid']])->find();

        if (empty($result)) {
            $member_data = array(
                'username' => $user_arr['nickname'],
                'img' => $this->getlocalimg($user_arr['headimgurl']),
                'addtime' => time(),
                'eqno' => 'zh_temple',
                'token' => md5('zh_temple' . $user_arr['unionid'] . config('home.token_halt') . time()),
                'tokentime' => time(),
                'sex' => $user_arr['sex']
            );
            if(!empty($param['comid'])){
                $member_data['register_comid'] = $param['comid'];
            }
            if(!Cookie::has($user_arr['unionid'])){
                Cookie::set($user_arr['unionid'],$user_arr['unionid'],60);
                Cookie::set($user_arr['unionid'].'token',$member_data['token'],60);
                $id = Db::name('member')->insertGetId($member_data);
                $wechat['memid'] = $id;
                $wechat['addtime'] = time();
                Db::name('wechat')->insertGetId($wechat);
                $has_tel = 2;
            }else{
                $member_data['token'] = Cookie::get($user_arr['unionid'].'token');
                $has_tel = 2;
            }
        } else {
            if(empty($result['memid'])){
                $member_data = array(
                    'username' => $user_arr['nickname'],
                    'img' => $this->getlocalimg($user_arr['headimgurl']),
                    'addtime' => time(),
                    'eqno' => 'zh_temple',
                    'token' => md5('zh_temple' . $user_arr['unionid'] . config('home.token_halt') . time()),
                    'tokentime' => time(),
                    'sex' => $user_arr['sex']
                );
                if(!empty($param['comid'])){
                    $member_data['register_comid'] = $param['comid'];
                }
                $id = Db::name('member')->insertGetId($member_data);
                $wechat['memid'] = $id;
                $has_tel = 2;
            }else {
                $member_data = Db::name('member')->where(['id' => $result['memid']])->find();
                if (empty($member_data)) {
                    $member_data = array(
                        'username' => $user_arr['nickname'],
                        'img' => $this->getlocalimg($user_arr['headimgurl']),
                        'addtime' => time(),
                        'eqno' => 'zh_temple',
                        'token' => md5('zh_temple' . $user_arr['unionid'] . config('home.token_halt') . time()),
                        'tokentime' => time(),
                        'sex' => $user_arr['sex']
                    );
                    if(!empty($param['comid'])){
                        $member_data['register_comid'] = $param['comid'];
                    }
                    $id = Db::name('member')->insertGetId($member_data);
                    $wechat['memid'] = $id;
                    $has_tel = 2;
                } else {
                    if (empty($member_data['tel'])) {
                        $has_tel = 2;
                    } else {
                        $has_tel = 1;
                    }
                    $id = $member_data['id'];
                }
            }
            Db::name('member')->where(['id'=>$id])->update(['tokentime'=>time()]);
            Db::name('wechat')->where(['id'=>$result['id']])->update($wechat);
        }
        return admin_json('1', '登录成功', ['token' => $member_data['token'], 'openid' => $user_arr['openid'],'has_tel'=>$has_tel]);
    }
    /**
     * [getIndex  微信授权登录]
     * @author [C。]
     * @return json
     */
    public function wechat_login($result,$user_arr)
    {
        $map = [];
        $map['id'] = $result['memid'];
        $mem_res = Db::name('member')->where($map)->find();
        if(empty($mem_res)){
            return admin_json('2', '请绑定手机号！', ['id' => $result['id']]);
        }
        if ($mem_res['thawingtime'] === 0) {
            return admin_json('1003', '您的账号已被永久封禁');
        }
        if ($mem_res['thawingtime'] > time()) {
            return admin_json('1003', "您的账号已被封禁，" . '解封日期为' . date('Y:m:d H:i:s', $mem_res['thawingtime']));
        }
        $darr = array();
//        $darr['img'] = $this->getlocalimg($user_arr['headimgurl'],$mem_res['id']);
//        $darr['username'] = $user_arr['nickname'];
//        $darr['sex'] = $user_arr['sex'];

        $nowtime = time();
        if (time() - $mem_res['tokentime'] > config('home.token_expire')) {
            $darr['token'] = md5('app' . $mem_res['tel'] . config('home.token_halt') . $nowtime);
        }else{
            $darr['token'] = $mem_res['token'];
        }

        $darr['tokentime'] = $nowtime;

        Db::name('member')->where('id', $mem_res['id'])->update($darr);
//        $imgarr = explode('/',$mem_res['img']);
//        if(!empty($imgarr[4])){
//            $result = 'headimg/F'.substr($mem_res['img'],strripos($mem_res['img'],"FS")+1);
//        }else{
//            $result = 'F'.substr($mem_res['img'],strripos($mem_res['img'],"FS")+1);
//        }
//        ossdel($result);
        return $darr['token'];
    }
    /**
     * [getIndex  微信授权登录]
     * @param type 1 验证码登陆  2密码登陆 3微信授权
     * @author [C。]
     * @return json
     */
    public function postIndex()
    {
        $param = input('param.');

        if ($param['type'] == 1) {
            $map = [];
            $map['tel'] = $param['tel'];
            $map['code'] = $param['code'];
            $result = Db::name('vercode')->where($map)->find();
            if (!empty($result)) {
                if ($result['status'] == 2) {
                    return admin_json('-3', '验证码已使用');
                }
                if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                    return admin_json('-2', '验证码已过期');
                }
                Db::name('vercode')->where($map)->update(['status' => 2]);
                $map1 = [];
                $map1['tel'] = $param['tel'];

                $mem = Db::name('member')->where($map1)->find();

                if (empty($mem)) {
                    $nowtime = time();
                    $darr = array();
                    $darr['username'] = '善友';
                    $darr['tel'] = $param['tel'];
                    $darr['addtime'] = $nowtime;
                    $darr['img'] = config('home.no_user_headimg');

                    $darr['tokentime'] = $nowtime;
                    $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt') . $nowtime);
                    $darr['eqno'] = $param['eqno'];
                    $darr['eqtype'] = $param['eqtype'];
                    $id = Db::name('member')->insertGetId($darr);
                    Db::name('member')->where('tel',$param['tel'])->update(['username'=>'善友'.$id]);
                } else {
                    if($mem['states'] == 3){
                        Db::name('member')->where('tel', $param['tel'])->update(['states'=>0]);
                    }
                    if ($mem['thawingtime'] === 0) {
                        return admin_json('1003', '您的账号已被永久封禁');
                    }
                    if ($mem['thawingtime'] > time()) {
                        return admin_json('1003', "您的账号已被封禁，" . '解封日期为' . date('Y:m:d H:i:s', $mem['thawingtime']));
                    }
                    if (time() - $mem['tokentime'] > config('home.token_expire')) {
                        $nowtime = time();
                        $darr = array();
                        $darr['tokentime'] = $nowtime;
                        $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt') . $nowtime);
                        Db::name('member')->where('tel', $param['tel'])->update($darr);
                    } else {
                        $darr['token'] = $mem['token'];
                    }
                }

                return admin_json('1', '登录成功', $darr['token']);
            } else {
                return admin_json('-1', '手机号或验证码错误');
            }
        } elseif ($param['type'] == 2) {
            $map = [];
            $map['tel'] = $param['tel'];
            $map['pswd'] = md5($param['pswd']);
            $map['states'] = ['neq',3];
            $result = Db::name('member')->where($map)->find();
            if (!empty($result)) {
                if ($result['states'] == 2) {
                    return admin_json('-1', '账号已冻结');
                }
                if ($result['thawingtime'] === 0) {
                    return admin_json('1003', '您的账号已被永久封禁');
                }
                if ($result['thawingtime'] > time()) {
                    return admin_json('1003', "您的账号已被封禁，" . '解封日期为' . date('Y:m:d H:i:s', $result['thawingtime']));
                }

                if (time() - $result['tokentime'] > config('home.token_expire')) {
                    $nowtime = time();
                    $darr = array();
                    $darr['tokentime'] = $nowtime;
                    $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt') . $nowtime);
                    Db::name('member')->where('tel', $param['tel'])->update($darr);
                } else {
                    $darr['token'] = $result['token'];
                }

                return admin_json('1', '登录成功', $darr['token']);
            } else {
                return admin_json('-2', '手机号或密码错误');
            }
        } elseif ($param['type'] == 3) {
//                try {
            $code = $param['code'];
            if ($param['app'] == 1) {
                $appid = config('home.kappid');
                $secret = config('home.ksecret');
            } elseif ($param['app'] == 2) {
                $appid = config('home.appid');
                $secret = config('home.secret');

            }elseif ($param['app'] == 3){
                $appid = config('home.wappid');
                $secret = config('home.wsecret');
            }

            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
            $info = file_get_contents($url);//发送HTTPs请求并获取返回的数据，推荐使用curl
            $json = json_decode($info);//对json数据解码
            $arr = get_object_vars($json);
            if (empty($arr['access_token'])) {
                return admin_json('-1', $arr);
            }
            $access_token = $arr['access_token'];
            $openid = $arr['openid'];
            $user_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
            $user_info = file_get_contents($user_url);//发送HTTPs请求并获取返回的数据，推荐使用curl
            $user_json = json_decode($user_info);//对json数据解码
            $user_arr = get_object_vars($user_json);
            $user_arr['nickname'] = filter_Emoji($user_arr['nickname']);
            $wechat = array(
                'openid' => $user_arr['openid'],
                'unionid' => $user_arr['unionid'],
                'headimgurl' => $user_arr['headimgurl'],
                'nickname' => $user_arr['nickname'],
                'sex' => $user_arr['sex'],
                'addtime' => time(),
            );
            $result = Db::name('wechat')->where(['unionid' => $user_arr['unionid']])->find();
            if (empty($result)) {
                $result = Db::name('wechat')->where(['openid' => $user_arr['openid']])->find();
                if(empty($result)){
                    $id = Db::name('wechat')->insertGetId($wechat);
                }else{
                    Db::name('wechat')->where(['id'=>$result['id']])->update($wechat);
                    $id = $result['id'];
                    if(!empty($result['memid'])){
                        $token = $this->wechat_login($result,$user_arr);
                        return admin_json('1', '登录成功', ['token' => $token, 'openid' => $user_arr['openid']]);
                    }
                }
                return admin_json('2', '请绑定手机号！', ['id' => $id,'type'=>2]);
            } else {
                Db::name('wechat')->where(['id'=>$result['id']])->update($wechat);
                if (empty($result['memid'])) {
                    return admin_json('2', '请绑定手机号！', ['id' => $result['id'],'type'=>2]);
                }
                $token = $this->wechat_login($result,$user_arr);
                return admin_json('1', '登录成功', ['token' => $token, 'openid' => $user_arr['openid']]);
            }
        }

    }
    /**
     * [getlocalimg 把微信头像存储起来]
     * @param
     * @author [C。]
     * @return json
     */
    public function getlocalimg($imgurl,$id = 0)
    {
        //取数组最后一个元素，得到文件类型
        $uploaddir = "wheader/";//设置文件保存目录 注意包含
        if (!file_exists($uploaddir)) {
            mkdir($uploaddir, 0755, true);
        }

        $header = array(
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
            'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding: gzip, deflate',);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $imgurl);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $data = curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if ($code == 200) {//把URL格式的图片转成base64_encode格式的！
            $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
        }
        $img_content=$imgBase64Code;//图片内容

        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result))
        {
            $type = $result[2];//得到图片类型png?jpg?gif?
            $new_file = $uploaddir . md5(uniqid(rand())) . '.' . $type; //产生随机文件名
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
            {
                $filename = uploadoss($new_file,'',$id);
                return $filename;
            }else{
                return $imgurl;
            }
        }else{
            return $imgurl;
        }
    }
    /**
     * [postWechatAuth 微信授权]
     * @param
     * @author [C。]
     * @return json
     */
    public function postWechatAuth()
    {
        $param = input('param.');
        if(empty($param['sex'])){
            $param['sex'] = 1;
        }
        $param['nickname'] = filter_Emoji($param['nickname']);
        $wechat = array(
            'openid' => $param['openid'],
            'unionid' => $param['unionid'],
            'headimgurl' => $param['headimgurl'],
            'nickname' => $param['nickname'],
            'sex' => $param['sex'],
            'addtime' => time(),
        );
        $result = Db::name('wechat')->where(['unionid' => $param['unionid']])->find();
        if (empty($result)) {
            $result = Db::name('wechat')->where(['openid' => $param['openid']])->find();
            if(empty($result)){
                $id = Db::name('wechat')->insertGetId($wechat);
            }else{
                Db::name('wechat')->where(['id'=>$result['id']])->update($wechat);
                $id = $result['id'];
            }
            return admin_json('2', '请绑定手机号！', ['id' => $id]);
        } else {
            Db::name('wechat')->where(['id'=>$result['id']])->update($wechat);
            if (empty($result['memid'])) {
                return admin_json('2', '请绑定手机号！', ['id' => $result['id']]);
            }
            $mem_res = Db::name('member')->where(['id' => $result['memid']])->find();
            if ($mem_res['thawingtime'] === 0) {
                return admin_json('1003', '您的账号已被永久封禁');
            }
            if ($mem_res['thawingtime'] > time()) {
                return admin_json('1003', "您的账号已被封禁，" . '解封日期为' . date('Y:m:d H:i:s', $mem_res['thawingtime']));
            }
            $darr = array();
//            $darr['img'] = $this->getlocalimg($param['headimgurl'],$result['memid']);
//            $darr['username'] = $param['nickname'];
            $nowtime = time();
            if (time() - $mem_res['tokentime'] > config('home.token_expire')) {
                $darr['token'] = md5('app' . $mem_res['tel'] . config('home.token_halt') . $nowtime);
                $darr['tokentime'] = $nowtime;
            } else {
                $darr['token'] = md5('app' . $mem_res['tel'] . config('home.token_halt') . $nowtime);
            }
            Db::name('member')->where('id', $mem_res['id'])->update($darr);
//            $result = 'headimg/F'.substr($mem_res['img'],strripos($mem_res['img'],"FS")+1);
//            ossdel($result);
        }
        return admin_json('1', '登录成功', ['token' => $darr['token']]);
    }
    /**
     * [postBindtel 绑定手机]
     * @param
     * @author [C。]
     * @return json
     */
    public function postBindtel()
    {
        $param = input('param.');
        $map = [];
        $map['tel'] = $param['tel'];
        $map['code'] = $param['code'];
        $map1 = [];
        $map1['tel'] = $param['tel'];

        $mem = Db::name('member')->where($map1)->find();
//        if(!empty($mem)){
//            return admin_json('-4','手机号已注册！');
//        }
        $result = Db::name('vercode')->where($map)->find();
        if(!empty($result)) {
            if ($result['status'] == 2) {
                return admin_json('-3', '验证码已使用');
            }
            if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                return admin_json('-2', '验证码已过期');
            }
            Db::name('vercode')->where($map)->update(['status' => 2]);
        }else{
            return admin_json('-1','手机号或验证码错误');
        }

        if(empty($param['id'])){
            if(empty($mem)) {
                $darr = array();
                $nowtime = time();
                $darr['pswd'] = md5($param['pswd']);
                $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt'));
                $darr['tokentime'] = $nowtime;
                $darr['eqno'] = $param['eqno'];
                $darr['eqtype'] = $param['eqtype'];

                $darr['username'] = '善友';
                $darr['tel'] = $param['tel'];
                $darr['addtime'] = $nowtime;
                $darr['img'] = config('home.no_user_headimg');
                $token = $darr['token'];
                if(!empty($param['uid'])){
                    $darr['agent_id'] = $param['uid'];
                }
                $id = Db::name('member')->insertGetId($darr);
                Db::name('member')->where('tel',$param['tel'])->update(['username'=>'善友'.$id]);
                if(!empty($param['uid'])){
                    $this->editJifen($param['uid'],$id);
                }
            }else{
                if($mem['states'] == 3){
                    Db::name('member')->where('tel', $param['tel'])->update(['states'=>0]);
                }else{
                    return admin_json('-4','手机号已注册！');
                }
            }
            return admin_json('1','注册成功',$token);
        }else{
            $wechat = Db::name('wechat')->where('id',$param['id'])->find();
            $darr = array();
            $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt'));

            $nowtime = time();
            $darr['pswd'] = md5($param['pswd']);
            $darr['username'] = $wechat['nickname'];
            $darr['tel'] = $param['tel'];
            $darr['addtime'] = $nowtime;
            $darr['img'] = $this->getlocalimg($wechat['headimgurl']);
            $darr['tokentime'] = $nowtime;
            $darr['eqno'] = $param['eqno'];
            $darr['eqtype'] = $param['eqtype'];
            if(!empty($param['uid'])){
                $darr['agent_id'] = $param['uid'];
            }
            if(empty($mem)){
                $id = Db::name('member')->insertGetId($darr);
                if(!empty($param['uid'])){
                    $this->editJifen($param['uid'],$id);
                }
            }else{
                $id = $mem['id'];
                if($mem['states'] == 3){
                    Db::name('member')->where('tel', $param['tel'])->update(['states'=>0]);
                }
            }
            $wechat = Db::name('wechat')->where('id',$param['id'])->find();
            if(empty($wechat['memid'])){
                Db::name('wechat')->where('id',$param['id'])->update(['memid'=>$id]);
            }

            return admin_json('1','绑定成功',$darr['token']);
        }
    }
    /**
     * [postForgetpswd 邀请成功修改福豆]
     * @param
     * @author [C。]
     * @return json
     */
    public function editJifen($uid,$id)
    {
        $mem = Db::name('member')->where('id',$uid)->find();
        if(!empty($mem)){
            $jifen = $mem['jifen'] + config('home.invite_jifen');
            Db::name('mjifen')->insert(['jifen'=>config('home.invite_jifen'),'memid'=>$uid,'addtime'=>time(),'bymid'=>$id]);
            Db::name('member')->where('id',$uid)->update(['jifen'=>$jifen]);
            add_quality($mem['id'],19,$mem['quality'],2);
        }
    }
    /**
     * [postForgetpswd 忘记密码]
     * @param
     * @author [C。]
     * @return json
     */
    public function postForgetpswd()
    {
        $param = input('param.');
        $map = [];
        $map['tel'] = $param['tel'];
        $map['code'] = $param['code'];
        $map1 = [];
        $map1['tel'] = $param['tel'];

        $mem = Db::name('member')->where($map1)->find();
        if(empty($mem)){
            return admin_json('-5', '手机号不存在！');
        }
        $result = Db::name('vercode')->where($map)->find();
        if(!empty($result)) {
            if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                return admin_json('-2', '验证码已过期');
            }
            if ($result['status'] == 2) {
                return admin_json('-3', '验证码已使用');
            }
            Db::name('vercode')->where($map)->update(['status' => 2]);
        }else{
            return admin_json('-1','手机号或验证码错误');
        }
        $pswd = md5($param['pswd']);
        if($pswd == $mem['pswd']){
            return admin_json('1','修改成功');
        }
        $mresult = Db::name('member')->where($map1)->update(['pswd' => $pswd]);
        if(!empty($mresult)){
            return admin_json('1','修改成功');
        }else{
            return admin_json('-4','修改失败');
        }
    }
    /**
     * [getCode 获取验证码]
     * @param
     * @author [C。]
     * @return json
     */
    public function postCode()
    {
        $param = input('param.');
        $tel = $param['tel'];
        $code = randString();

        $result = demo_sendsms($tel,$code);
        if ($result && 'OK' == $result['Code']) {
            $data = array(
                'tel' => $tel,
                'code' => $code,
                'type' => $param['type'],
                'addtime' =>time()
            );
            Db::name('vercode')->insert($data);
            return admin_json('1','验证码已发送');
        }else{
            return admin_json('-1','请稍后再试！');
        }
    }
    /**
     * [postVeriTel 验证手机]
     * @param
     * @author [C。]
     * @return json
     */
    public function postVeriTel()
    {
        $param = input('param.');
        $map = [];
        $map['tel'] = $param['tel'];
        $map['code'] = $param['code'];
        $map1 = [];
        $map1['tel'] = $param['tel'];
        $mem = Db::name('member')->where($map1)->find();
        if(empty($mem)){
            return admin_json('-5', '手机号不存在！');
        }
        $result = Db::name('vercode')->where($map)->find();
        if(!empty($result)) {
            if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                return admin_json('-2', '验证码已过期');
            }
            if ($result['status'] == 2) {
                return admin_json('-3', '验证码已使用');
            }
            Db::name('vercode')->where($map)->update(['status' => 2]);
        }else{
            return admin_json('-1','手机号或验证码错误');
        }
        return admin_json('1','验证成功');
    }
    /**
     * [postEdittel 换绑手机]
     * @param
     * @author [C。]
     * @return json
     */
    public function postEditTel()
    {
        $param = input('param.');
        $map = [];
        $map['tel'] = $param['tel'];
        $map['code'] = $param['code'];
        $map1 = [];
        $map1['tel'] = $param['tel'];

        $mem = Db::name('member')->where($map1)->find();
        if(!empty($mem)){
            return admin_json('-5', '该手机号已绑定用户！');
        }
        $result = Db::name('vercode')->where($map)->find();
        if(!empty($result)) {
            if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                return admin_json('-2', '验证码已过期');
            }
            if ($result['status'] == 2) {
                return admin_json('-3', '验证码已使用');
            }
            Db::name('vercode')->where($map)->update(['status' => 2]);
        }else{
            return admin_json('-1','手机号或验证码错误');
        }
        $member = Db::name('member')->where('token',$param['token'])->find();
        $manager = Db::name('manager')->where('tel',$member['tel'])->find();

        if(!empty($manager)){
            Db::name('manager')->where('id',$manager['id'])->update(['tel'=>$param['tel']]);
            $mpass = Db::name('manager_password')->where('mid',$manager['id'])->find();
            if(!empty($mpass)){
                Db::name('manager_password')->where('mid',$manager['id'])->update(['tel'=>$param['tel']]);
            }
        }

        $mresult = Db::name('member')->where('id',$member['id'])->update(['tel'=>$param['tel']]);
        if(!empty($mresult)){
            return admin_json('1','修改成功');
        }else{
            return admin_json('-4','修改失败');
        }
    }
    /**
     * [postInviteBind 邀请注册会员]
     * @param
     * @author [C。]
     * @return json
     */
    public function postInviteBind()
    {
        $param = input('param.');
        $map = [];
        $map['tel'] = $param['tel'];
        $map['code'] = $param['code'];
        try{
            $result = Db::name('vercode')->where($map)->find();
            if(!empty($result)) {
                if ($result['status'] == 2) {
                    return admin_json('-3', '验证码已使用');
                }
                if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                    return admin_json('-2', '验证码已过期');
                }
            }else{
                return admin_json('-1','手机号或验证码错误');
            }
            $map1 = [];
            $map1['tel'] = $param['tel'];
            $mem = Db::name('member')->where($map1)->find();
            if(!empty($mem))
                return admin_json('-5', '该手机已经注册！');

            $wechat = Db::name('wechat')->where('id',$param['id'])->find();
            $darr = array();
            $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt'));

            $nowtime = time();
            $darr['pswd'] = md5($param['pswd']);
            $darr['username'] = $wechat['nickname'];
            $darr['tel'] = $param['tel'];
            $darr['addtime'] = $nowtime;
            $darr['img'] = $wechat['headimgurl'];
            $darr['tokentime'] = $nowtime;
            $darr['eqno'] = $param['eqno'];
            $darr['eqtype'] = $param['eqtype'];

            if(!empty($param['agent_id'])) {
                $darr['agent_id'] = $param['agent_id'];
            }

            $id = Db::name('member')->insertGetId($darr);
            Db::name('wechat')->where('id',$param['id'])->update(['memid'=>$id]);

            if(!empty($param['agent_id'])){
                $agent = Db::name('member')->where('id',$param['agent_id'])->find();
                if(!empty($agent)){
                    $jifen = $agent['jifen'] + config('home.invite_jifen');
                    Db::name('member')->where('id',$agent['id'])->update(['jifen'=>$jifen]);
                }
            }
        } catch (\Exception $e) {
            return admin_json(-1, $e->getMessage());
        }
        Db::name('vercode')->where($map)->update(['status' => 2]);
        return admin_json('1','绑定成功',$darr['token']);

    }
}